package com.lanou3g.dllo.baidumusic.welcome;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseActivity;
import com.lanou3g.dllo.baidumusic.base.MyApp;
import com.lanou3g.dllo.baidumusic.database.MyFavorite;
import com.lanou3g.dllo.baidumusic.database.SingleLiteOrm;
import com.lanou3g.dllo.baidumusic.main.playmusic.PlaymusicBean;
import com.lanou3g.dllo.baidumusic.mine.myfavorite.MyFavoriteManager;
import com.lanou3g.dllo.baidumusic.tools.PlaylistManager;
import com.litesuits.orm.LiteOrm;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by dllo on 16/6/22.
 */
public class WelcomeActivity extends BaseActivity {

    private TextView welcomeTv;
    private ImageView welcomeIv;
    private CountDownTimer timer;
    private MyFavorite myFavorite;

    @Override
    public int setLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initView() {

        welcomeTv = (TextView) findViewById(R.id.welcome_time_tv);
        welcomeIv = (ImageView) findViewById(R.id.welcome_show_iv);

    }

    @Override
    public void initData() {

        timer = new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                welcomeTv.setText(millisUntilFinished / 1000 + "s");
                welcomeTv.setVisibility(View.GONE);
            }

            @Override
            public void onFinish() {
                // 跳转到mainActivity动画方法
                intentMainActivityAnimation();
                finish();
            }
        }.start();

        // 查询正在播放列表里的数据
        PlaylistManager.getInstance().getPlaylist().clear();
        for (PlaymusicBean bean : SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(PlaymusicBean.class)) {
            PlaylistManager.getInstance().getPlaylist().add(bean);
        }

        // 查询当前用户信息
        BmobUser bmobUser = BmobUser.getCurrentUser(this);
        if (bmobUser != null) {
            myFavorite = new MyFavorite();
            BmobQuery<MyFavorite> bmobQuery = new BmobQuery<>();
            bmobQuery.addWhereEqualTo("userName", bmobUser.getUsername());
            bmobQuery.findObjects(this, new FindListener<MyFavorite>() {
                @Override
                public void onSuccess(List<MyFavorite> list) {
                    for (MyFavorite myFavorite : list) {
                        MyFavoriteManager.getInstance().getMyFavorites().add(myFavorite);
                    }

                    int number = 0;
                    for (MyFavorite song : SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(MyFavorite.class)) {
                        for (MyFavorite favoriteSong : MyFavoriteManager.getInstance().getMyFavorites()) {
                            if (song.getTitle().equals(favoriteSong.getTitle())) {
                                number++;
                            }
                        }

                        if (number == 0) {
                            song.setUserName(BmobUser.getCurrentUser(MyApp.context).getUsername());
                            // save:实现单个对象增加
                            song.save(MyApp.context, new SaveListener() {
                                @Override
                                public void onSuccess() {

                                }

                                @Override
                                public void onFailure(int i, String s) {

                                }
                            });
                        } else {
                            number = 0;
                        }

                    }
                    // 调用获取网络数据的方法
                    getNetData();
                }

                @Override
                public void onError(int i, String s) {

                }
            });
        }


    }

    // 获取网络数据
    public void getNetData() {
        if (BmobUser.getCurrentUser(MyApp.context) != null) {
            BmobQuery<MyFavorite> bmobQuery = new BmobQuery<>();
            bmobQuery.addWhereEqualTo("userName", BmobUser.getCurrentUser(MyApp.context).getUsername());
            bmobQuery.findObjects(this, new FindListener<MyFavorite>() {
                @Override
                public void onSuccess(List<MyFavorite> list) {
                    MyFavoriteManager.getInstance().getMyFavorites().clear();
                    for (MyFavorite myFavorite : list) {
                        MyFavoriteManager.getInstance().getMyFavorites().add(myFavorite);
                    }
                }

                @Override
                public void onError(int i, String s) {

                }
            });
        }
    }
}
